<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2011-12-23 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
				
		$this->excute();
	}
	
	function excute()
	{
		ob_start();
		
		switch($this->act)
		{
			case 'do_delete':
				$this->doDelete();
				break;
			case 'modify':
				$this->modify();
				break;
			case 'do_modify':
				$this->doModify();
				break;
			default:
				$this->main();
				break;
		}
		
		$body = ob_get_clean();
		
		$this->showBody($body);
	}
	
	private function main()
	{
		$page = $this->page_num + 1;
		
		$type = isset($this->request['type']) ? $this->request['type'] : '';
		
		$review_id = isset($this->request['review_id']) ? (int)$this->request['review_id'] : '';
		
		$keyword = isset($this->request['keyword']) ? trim($this->request['keyword']) : '';
		$up1 = (isset($this->request['up1']) && $this->request['up1']) ? (int)$this->request['up1'] : '';
		$from_time = isset($this->request['from_time']) ? trim($this->request['from_time']) : '';
		$to_time = isset($this->request['to_time']) ? trim($this->request['to_time']) : '';
		
		//$query_link = "admin.php?m=review_reply&page={$page}&count={$this->page_limit}";
		$query_link = "admin.php?m=review_reply";
		$where_list = array();
		
		if($review_id)
		{
			$where_list['rid'] = "movie_review_id = '$review_id'";
			$query_link .= "&review_id=".urlencode($review_id);
		}
		if($keyword)
		{
			$where_list['keyword'] = "lower(`content`) like lower('%$keyword%')";
			$query_link .= "&keyword=".urlencode($keyword);
		}
		if($up1)
		{
			$where_list['up1'] = "up_count > '$up1'";
			$query_link .= "&up1=".urlencode($up1);
		}
		
		if(strtotime($from_time) !== false)
		{
			$time = strtotime($from_time);
			//$where_list['from_time'] = "release_date > '$time'";
			$query_link .= "&from_time=".urlencode($from_time);
		}
		else
		{
			$from_time = date('Y-m-d', time() - 3600*24*30);
		}
		
		if(strtotime($to_time) !== false)
		{
			$time = strtotime($to_time);
			//$where_list['to_time'] = "release_date < '$time'";
			$query_link .= "&to_time=".urlencode($to_time);
		}
		else
		{
			$to_time = date('Y-m-d');
		}
		
		$where = empty($where_list) ? '' : 'where '.implode(' and ', $where_list).' ';
		
		$sql = " select count(1) as `total_record` from `".TABLE_PREFIX."movie_review_reply` {$where}";
		$total_record = DBFunction::getInstance()->getOne($sql);

		$item_list = array();
		if($total_record > 0)
		{
			$offset = $this->page_num * $this->page_limit;
			$sql = "select *from `".TABLE_PREFIX."movie_review_reply`
					{$where}
					order by `id` desc limit {$offset},{$this->page_limit} ";
			$item_list = DBFunction::getInstance()->getAll($sql);
		}
		
		$page_html = page($total_record, $this->page_limit, $page, $query_link, '20 50 100 200 500');
		
		include template('admin/movie_review_reply');
	}
	
	private function modify()
	{
		$review_reply_id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$review_reply_id)
		{
			$this->messager("请指定ID", 'admin.php?m=review_reply');
		}
		
		$review_reply = get_movie_review_reply_info($review_reply_id);
		
		if(!isset($review_reply['id']) || !$review_reply['id'])
		{
			$this->messager("回复不存在", 'admin.php?m=review_reply');
		}
		
		$action = 'do_modify&id='.$review_reply['id'];
		$action_name = '影评回复修改';
		
		include template('admin/movie_review_reply_info');
	}
	
	private function doModify()
	{
		$request['id'] = isset($this->request['id']) ? (int)$this->request['id'] : '';
		$request['content'] = isset($this->request['content']) ? trim($this->request['content']) : '';
		
		if(!$request['id'])
		{
			$this->messager("请指定ID", 'admin.php?m=review_reply');
		}
		if(!$request['content'])
		{
			$this->messager("内容不能为空", 'admin.php?m=review_reply&a=modify&id='.$request['id']);
		}
		
		$review_reply = get_movie_review_reply_info($request['id']);
		
		if(!isset($review_reply['id']) || !$review_reply['id'])
		{
			$this->messager("回复不存在", 'admin.php?m=review_reply');
		}
		
		$result = update_movie_review_reply($request);
		
		if($result)
		{
			$this->messager("修改成功", 'admin.php?m=review_reply');
		}
		else
		{
			$this->messager("修改失败", 'admin.php?m=review_reply&a=modify&id='.$request['id']);
		}
	}
	
	private function doDelete()
	{
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		if(check_array($ids))
		{
			$ids = implode(',', $ids);
		}
		
		if(!$ids)
		{
			$this->messager("请指定ID", 'admin.php?m=review_reply');
		}
		
		$result = delete_movie_review_reply($ids);
		
		if($result)
		{
			$this->messager("删除成功", 'admin.php?m=review_reply');
		}
		else
		{
			$this->messager("删除失败", 'admin.php?m=review_reply');
		}
	}
}